import BaseSprite from "../Base/BaseSprite";

enum EAnimType {
    Rotate = 0,
    Jelly,
    Vertical,   // 垂直上下
    Hand,       // 小手点击
    Breath,     // 呼吸
}

const { ccclass, property } = cc._decorator;

@ccclass
export class Animation extends BaseSprite {

    @property({ step: 1, tooltip:"0.旋转\n1.果冻\n2.上下\n3.小手点击\n4.呼吸" })
    private type: number = 0;

    @property({ step: 1, tooltip: "延时x秒后执行" })
    private delayTime: number = 0;

    protected onLoad() { }

    protected start() {
        const cb = () => {
            switch (this.type) {
                case EAnimType.Rotate:
                    {
                        cc.tween(this.node).repeatForever(
                            cc.rotateBy(3.5, 360),
                        ).start();
                    }
                    break;
                case EAnimType.Jelly:
                    {
                        this.effectJelly(3);
                    }
                    break;
                case EAnimType.Vertical:
                    {
                        const distance = 25;
                        const pos = this.node.getPosition();
                        cc.tween(this.node).repeatForever(
                            cc.sequence(
                                cc.moveTo(0.5, cc.v2(pos.x, pos.y + distance)),
                                cc.moveTo(1, cc.v2(pos.x, pos.y - distance)),
                                cc.moveTo(0.5, cc.v2(pos.x, pos.y)),
                            ),
                        ).start();
                    }
                    break;
                case EAnimType.Hand:
                    {
                        cc.tween(this.node).repeatForever(
                            cc.sequence(
                                cc.rotateTo(0.3, -25), cc.rotateTo(0.3, 0),
                                cc.rotateTo(0.3, -25),
                                cc.delayTime(0.3),
                                cc.rotateTo(0.3, 0),
                            ),
                        ).start();
                    }
                    break;
                case EAnimType.Breath:
                    {
                        cc.tween(this.node).repeatForever(
                            cc.sequence(
                                cc.scaleTo(0.75, 1.2),
                                cc.scaleTo(0.75, 0.8),
                            ),
                        ).start();
                    }
                    break;
            }
        }

        cc.tween(this.node).delay(this.delayTime).call(
            () => {
                cb && cb();
            },
        ).start();
    }


    /** 果冻状效果 */
    public effectJelly(delayTime: number = 0) {
        const scaleTime = 0.15;
        cc.tween(this.node).repeatForever(
            cc.sequence(
                cc.delayTime(delayTime),
                cc.scaleTo(scaleTime, 1, 1),
                // cc.scaleTo(scaleTime, 1.25, 0.75), cc.scaleTo(scaleTime, 0.75, 1.25),
                cc.scaleTo(scaleTime, 1.20, 0.80), cc.scaleTo(scaleTime, 0.8, 1.20),
                cc.scaleTo(scaleTime, 1.15, 0.85), cc.scaleTo(scaleTime, 0.85, 1.15),
                cc.scaleTo(scaleTime, 1.10, 0.90), cc.scaleTo(scaleTime, 0.90, 1.10),
                cc.scaleTo(scaleTime, 1.05, 0.95), cc.scaleTo(scaleTime, 1, 1),
                cc.delayTime(delayTime),
            )
        ).start();
    }
}